﻿using Common.HttpRequest;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AppConfig;

namespace Common.Swagger
{
    /// <summary>
    /// 根据swagger生成的json文件读取现在url所在action的注释
    /// </summary>
    public static class SwaggerReadSummary
    {
        private static Dictionary<string, string> appSwagger = new Dictionary<string, string>();

        public static UrlSummaryEntity ReadSummary(HttpContext context)
        {
            string url = context.Request.Path.Value.ToLower();
            if (url == "/" || url.EndsWith("swagger.json") || url.Contains("/swagger/"))
            {
                return null;
            }
            string appName = url.Split('/', StringSplitOptions.RemoveEmptyEntries)[0];
            string swaggerjsonurl = string.Empty;
            string controllerName = string.Empty;
            string name = string.Empty;
            if (ApplicationConfig.ApplicationEntitys.FirstOrDefault(t => t.ServiceName == appName) == null)
            {
                appName = ApplicationConfig.Gateway.ServiceName;
                swaggerjsonurl = context.Request.Scheme + "://" + context.Request.Host.Value + "/swagger/v1/swagger.json";
            }
            else
            {
                swaggerjsonurl = context.Request.Scheme + "://" + context.Request.Host.Value + "/" + appName + "/swagger/v1/swagger.json";
            }
            string appUrl = url;//.Replace("/" + appName, "");            
            if (!appSwagger.ContainsKey(appName))
            {
                string jsonstr = CustomerHttpRequest.RestGet(swaggerjsonurl);
                appSwagger.Add(appName, jsonstr);
            }
            if (appSwagger.ContainsKey(appName) && !string.IsNullOrEmpty(appSwagger[appName]))
            {
                Dictionary<string, object> swaggerRoot = JsonConvert.DeserializeObject<Dictionary<string, object>>(appSwagger[appName]);
                Dictionary<string, object> paths = JsonConvert.DeserializeObject<Dictionary<string, object>>(swaggerRoot["paths"].ToString());
                if (paths.ContainsKey(appUrl))
                {
                    Dictionary<string, Dictionary<string, object>> keyValues = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, object>>>(paths[appUrl].ToString());

                    Dictionary<string, object> pathinfo = keyValues.FirstOrDefault().Value;
                    if (pathinfo.ContainsKey("summary"))
                    {
                        name = pathinfo["summary"].ToString();
                    }
                    if (pathinfo.ContainsKey("tags"))
                    {
                        controllerName = pathinfo["tags"].ToString().Trim(new char[] { '[', ']', '\"','\r','\n',' ' });
                    }
                }
            }
            return new UrlSummaryEntity() { ApplicationName = appName, ControllerName = controllerName, Name =  name};
        }
    }
    public class UrlSummaryEntity
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ApplicationName { get; set; }
        /// <summary>
        /// Controller名称
        /// </summary>
        public string ControllerName { get; set; }
        /// <summary>
        /// 接口action描述
        /// </summary>
        public string Name { get; set; }
    }
}
